package com.vturkey.struts.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.vturkey.beans.UserBean;
import com.vturkey.model.User;
 
@Action(value = "welcome", results = {
        @Result(name = "SUCCESS", location = "/website/welcome.jsp"),
        @Result(name = "ERROR", location = "/website/error.jsp") })
@Namespaces(value={@Namespace("/")})
public class WelcomeAction {
 
	@Autowired
	private UserBean userBean;
	
    public String execute() throws Exception {
    	
    	//1- First way to get spring bean  
    	//WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
    	//userBean= (UserBean) context.getBean("userBean");
    	//2- Second way to load spring bean 
    	SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    	//3- Third way is by creating struts action in spring and refer it in struts.xml see --> http://www.java2blog.com/2012/09/struts-2-spring-3-integration-example.html
    	userBean.saveUser(new User()); 
        return "SUCCESS";
    }
     
    //Java Bean to hold the form parameters
    private String name;
    private String pwd;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    
	public UserBean getUserBean() {
		return userBean;
	}
	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
    
}